<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="true" name="HandheldFriendly">
    <title></title>
    <meta content="" name="keywords"/>
    <meta content="" name="description"/>
    <link href="/web/css/normalize.css" rel="stylesheet" type="text/css"/>
    <link href="/web/css/style.css" rel="stylesheet" type="text/css"/>
    <link href="/web/css/animate.css" rel="stylesheet" type="text/css"/>
    <link href="/web/css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
    <link href="/web/layui/css/layui.css" rel="stylesheet" type="text/css"/>
    <style>
        [v-cloak] {
            display: none;
        }
    </style>
</head>
<body>
<div id="app" v-cloak>
    <div class="contain">
        <div class="sidebar" id="head">

        </div>
        <section>
            <div class="right_h" id="right_h">

            </div>
            <div class="qulicklink">
                <ul>
                    <li><a href="javascript:;"></a></li>
                    <li><a href="javascript:;"><span>企业首页</span><img src="/web/images/close.png"></a></li>
                    <li class="dq"><a href="javascript:;"><span>业务管理</span><img src="/web/images/close.png"></a></li>
                    <li><a href="javascript:;"></a></li>
                </ul>
            </div>
            <div class="breadcrumbs">
                <ul>
                    <li>系统设置</li>
                    <li>用户列表</li>
                </ul>
            </div>
            <div class="clearfix"></div>
            <div class="content">
                <div class="addbtn" v-if="sum(63)">
                    <a @click="addbtnClick()" href="javascript:;">新增</a>
                </div>
                <div class="deletebtn">
                    <a @click="deleteAllClick()" href="javascript:;">删除</a>
                </div>
                <div class="adSearchbtn" v-if="sum(1101)">
                    <a @click="adSearchbtnClick()" href="javascript:;">高级搜索</a>
                </div>

                <!--高级搜索下拉框开始-->
                <div class="searchBox">
                    <div class="searchKey">
                        <label>
                            <span>用户名称:</span> <input autocomplete="off" id="serachusername" name="resetval"
                                                      placeholder="请输入用户名称" type="text"/>
                        </label>
                        <label>
                            <span>所属部门:</span>
                            <div class="layui-input-block">
                                <select class="category" id="searchcharacter" lay-filter="batch"
                                        lay-search="required" lay-verify="required" name="resetval">
                                    <option class="promptColor" selected value="0">请选择所属部门</option>
                                    <option :value="v.id" v-for="(v,index) in ajaxData.character">
                                        {{v.title}}
                                    </option>
                                </select>
                            </div>
                        </label>
                    </div>
                    <div class="btns">
                        <div class="addbtn searchbtn">
                            <a @click="searchbtnClick()" href="javascript:;">搜索</a>
                        </div>
                        <div class="addbtn setbtn">
                            <a @click="resetbtnClick()" href="javascript:;">重置</a>
                        </div>
                    </div>
                </div>
                <!--高级搜索下拉框结束-->

                <div class="search">
                    <!--模糊查询开始-->
                    <label>
                        <span>用户名称:</span>
                        <div class="searchbox">
                            <input @input="search($event)" placeholder="请输入您要搜索的姓名" type="text">
                        </div>
                    </label>
                    <!--模糊查询结束-->
                </div>
            </div>
            <!--这里是新增编辑弹出框开始-->
            <div class="addCharacter hidden">
                <div class="addtitle">基本信息</div>
                <div class="layerbody">
                    <label>
                        <span>用户名:</span> <input autocomplete="off" class="username" name="resetTc" placeholder="请输入用户名"
                                                   type="text"/>
                    </label>
                    <label>
                        <span>密码:</span> <input autocomplete="off" class="password" name="resetTc"
                                                 placeholder="请输入密码" type="password"/>
                    </label>
                    <label>
                        <span>姓名:</span> <input autocomplete="off" class="realname username" name="resetTc"
                                                 placeholder="请输入姓名" type="text"/>
                    </label>
                    <label>
                        <span>性别:</span>
                        <input checked class="radio radio1" name="sex" type="radio" value="1"/> 男
                        <input class="radio" id="nv" name="sex" type="radio" value="0"> 女
                    </label>
                    <div class="usercontact">
                        <label>
                            <span>手机:</span> <input autocomplete="off" class="phone" maxlength="11" name="resetTc"
                                                     placeholder="请输入手机" type="text"/>
                        </label>
                        <label>
                            <span>邮箱:</span> <input autocomplete="off" class="email" name="resetTc" placeholder="请输入邮箱地址"
                                                     type="text"/>
                        </label>
                    </div>
                    <div class="bumenzhiwei">
                        <label>
                            <span>所属部门:</span>
                            <div class="layui-input-block">
                                <select id="departPosition" lay-filter="batch" lay-search="required"
                                        lay-verify="required" name="resetTc" v-model="characterid">
                                    <option class="promptColor" value="0">请选择所属部门</option>
                                    <option :key="index" :value="v.id" v-for="(v,index) in departmentList">
                                        {{v.title}}
                                    </option>
                                </select>
                            </div>
                        </label>
                    </div>
                    <label>
                        <span>状态:</span>
                        <input checked class="radio radio1" name="status" type="radio" value="1"/> 有效
                        <input class="radio" id="wuxiao" name="status" type="radio" value="0"> 无效
                    </label>
                </div>
                <div class="userlistbtn">
                    <button @click="addsureBtn()" class="surebtn" type="submit">确认</button>
                    <button @click="addresetBtn()" class="resetbtn" type="submit">重置</button>
                </div>
            </div>
            <input id="editorId" type="hidden" value=""/>
            <!-- 这里是新增编辑弹出框结束-->
            <div class="t_body">
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                        <tr>
                            <th><input @click="deleteCheckAll()" class="checkboxAll" id="deleteCheckAll"
                                       type="checkbox"> 全选
                            </th>
                            <th>用户名</th>
                            <th>姓名</th>
                            <th>性别</th>
                            <th>手机</th>
                            <th>邮箱</th>
                            <th>所属部门</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr :key="index" @dblclick="editorClick(v.id)" v-for="(v,index) in tableDataList">
                            <td v-if="v.id == 1"></td>
                            <td v-else><input :id="'listcheck' + (v.id)" class="selectCheck" type="checkbox"></td>
                            <td>{{v.username}}</td>
                            <td>{{v.realname}}</td>
                            <td v-if="v.sex == 1">男</td>
                            <td v-else>女</td>
                            <td>{{v.phone}}</td>
                            <td>{{v.email}}</td>
                            <td>{{v.department}}</td>
                            <td style="color: #f00" v-if="v.status == 0">无效</td>
                            <td style="color: #31c37a" v-else>有效</td>
                            <td v-if="v.id == 1">
                                超级管理员不可操作
                            </td>
                            <td v-else>
                                <div class="editor" v-if="sum(64)">
                                    <a @click="editorClick(v.id)" href="javascript:;">
                                        <img src="images/editor.png">
                                    </a>
                                    <span>编辑</span>
                                </div>
                                <div class="deletes" v-if="sum(65)">
                                    <a @click="deletesClick(v.id)" href="javascript:;">
                                        <img src="images/deletes.png">
                                    </a>
                                    <span>删除</span>
                                </div>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
                <!---------------------------分页start----------------------------->
                <div class="pageFen" v-if="totalNum>1">
                    <ul>
                        <li>
                            <a @click="setPage(curNum-1)" class="pageLink" href="#">上一页</a>
                        </li>
                        <li v-for="n in totalNum">
                            <a class="pageLink curNum" v-if="n == curNum" v-text="n"></a>
                            <a @click="setPage(n)" class="pageLink" href="javascript:;" v-else v-text="n"></a>
                        </li>
                        <li><a @click="setPage(curNum+1)" class="pageLink" href="#">下一页</a></li>
                    </ul>
                </div>
                <!---------------------------分页end-------------------------------->
            </div>
        </section>
    </div>
</div>
<script src="/web/js/vue.js"></script>
<script src="/web/js/jquery-1.10.0.min.js"></script>
<script src="/web/layui/layui.js"></script>
<script src="/web/js/transform.js"></script>
<script>
    AppData = new Vue({
        el: '#app',
        data: {
            tableData: {},
            categoryIndex: 0,
            show: false,
            tableDataList: {},
            totalNum: 0, //总页数
            listRows: 8, //每页数量
            curNum: 1, //当前页码
            departmentList: {},
            ajaxData: {},
            map: {},
            dataList: {},
            characterid: 2
        },
        mounted() {
            $("#head").load("header.html");
            $("#right_h").load("right.html");
            this.getIndexData();
        },
        methods: {
            verification(data) {
                if (data.status == 2){
                    layer.msg(data.message, {icon: 2, time: 1000}, function () {
                        window.location.href = "login.html";
                    });
                }
            },
            addbtnClick() {
                this.addresetBtn();
                $('#editorId').val('');
                layeraddoredit = layer.open({
                    type: 1,
                    title: '基本设置',
                    maxmin: true,
                    resize: true,
                    shadeClose: false, //点击遮罩关闭层
                    area: ['900px', '600px'],
                    content: $('.addCharacter'),
                    success: function () {
                        $.ajax({
                            type: "GET",
                            dataType: "json",
                            url: '/user/create',
                            data: {},
                            success: function (data) {
                                AppData.verification(data);
                                console.log(data.character);
                                let departmentList = {...data.character};
                                AppData.departmentList = departmentList;
                            },
                            error: function (msg) {
                                console.log(msg);
                            }
                        });
                    },
                    end: function () { //解决弹窗关闭以后,弹窗内容还出现在下方的问题
                        $('.addCharacter').hide();
                    }
                })
            },
            addsureBtn() {
                //输入框验证
                //验证用户名
                let names = $('.username').val();
                let regname = /^([\u4e00-\u9fa5]{1,20}|[a-zA-Z\.\s]{1,20})$/;
                if (names == '') {
                    layer.msg('请输入用户名');
                    return false;
                }
                if (!regname.test(names)) {
                    layer.msg('用户名格式填写错误，请重新填写');
                    return false;
                }
                //验证手机号码
                let phones = $('.phone').val();
                let regphone = /^[1][3,4,5,7,8,9][0-9]{9}$/;
                if (phones == '') {
                    layer.msg('请输入您的手机号码');
                    return false;
                }
                if (!regphone.test(phones)) {
                    layer.msg('手机格式不正确，请重新填写');
                    return false;
                }
                //密码验证
                let password = $('.password').val();
                if ($('.password').val() != '') {
                    let regpassword = /^(\w){6,20}$/;
                    if (password == '') {
                        layer.msg('请输入您的密码');
                        return false;
                    }
                    if (!regpassword.test(password)) {
                        layer.msg('请输入6-20个字母、数字、下划线组成的密码');
                        return false;
                    }
                }
                //邮箱验证
                let email = $('.email').val();
                let regemail = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
                if (email == '') {
                    layer.msg('请输入您的邮箱');
                    return false;
                }
                if (!regemail.test(email)) {
                    layer.msg('邮箱格式不正确，请重新填写');
                    return false;
                }
                let realname = $('.realname').val();
                let sexstatus = $('input[name="sex"]:checked').val();
                let status = $('input[name="status"]:checked').val();
                let options = $("#departPosition option:selected");
                let department = options.text();
                let departmentId = [];
                departmentId.push($("#departPosition").val());
                let editorId = $('#editorId').val();
                if (editorId) {
                    $.ajax({
                        type: "PUT",
                        dataType: "json",
                        url: '/user/' + editorId,
                        data: {
                            'username': names,
                            'password': password,
                            'realname': realname,
                            'sex': sexstatus,
                            'phone': phones,
                            'email': email,
                            'department': department,
                            'status': status,
                            'character': departmentId
                        },
                        success: function (data) {
                            AppData.verification(data);
                            if (data.status == 1) {
                                layer.msg(data.message, {icon: 1, time: 1000}, function () {
                                    AppData.getIndexData();
                                    layer.close(layeraddoredit);
                                });
                            } else {
                                layer.msg(data.message, {icon: 2, time: 1000});
                            }
                        },
                        error: function (msg) {
                            console.log(msg);
                        }
                    });
                } else {
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: '/user',
                        data: {
                            'username': names,
                            'password': password,
                            'realname': realname,
                            'sex': sexstatus,
                            'phone': phones,
                            'email': email,
                            'department': department,
                            'status': status,
                            'character': departmentId
                        },
                        success: function (data) {
                            AppData.verification(data);
                            if (data.status == 1) {
                                layer.msg(data.message, {icon: 1, time: 1000}, function () {
                                    AppData.getIndexData();
                                    layer.close(layeraddoredit);
                                });
                            } else {
                                layer.msg(data.message, {icon: 2, time: 1000});
                            }
                        },
                        error: function (msg) {
                            console.log(msg);
                        }
                    });
                }
            },
            deleteCheckAll() {//全选按钮
                let checkBoxs = AppData.tableDataList;
                if ($('#deleteCheckAll').is(":checked")) {
                    for (let j in checkBoxs) {
                        $('#listcheck' + checkBoxs[j].id).prop('checked', true);
                    }
                } else {
                    for (let j in checkBoxs) {
                        $('#listcheck' + checkBoxs[j].id).prop('checked', false);
                    }
                }
            },
            setPage(num) {
                //页码小于等于1
                if (num < 1) {
                    num = 1;
                }
                //页码大于总页数
                if (num > this.totalNum) {
                    num = this.totalNum;
                }
                //页码赋给当前页
                this.curNum = num;
                getIndexData();
            },
            search(e) {
                var that = this;
                var keywords = e.target.value;
                this.map.title = keywords;
                this.getIndexData();
            },
            searchbtnClick() {
                //获取所有搜索关键值
                var map = {
                    title: $('#serachusername').val(),
                    characterid: $('#searchcharacter').val()
                };
                this.map = map;
                this.getIndexData();
            },
            sum(sumid) {//验证列表里的修改删除是否要显示(根据对应的id)
                for (i in this.dataList) {
                    if (sumid == this.dataList[i].id) {
                        return true;
                    }
                }
                return false;
            },
            editorClick(edid) {
                if (edid == 1) {
                    layer.msg('超级管理员不可操作', {icon: 2, time: 1000}, function () {
                    });
                    return false;
                }
                $('#editorId').val(edid);
                layeraddoredit = layer.open({
                    type: 1,
                    title: '编辑菜单',
                    maxmin: true,
                    resize: true,
                    shadeClose: false, //点击遮罩关闭层
                    area: ['900px', '600px'],
                    content: $('.addCharacter'),
                    success: function () {
                        $.ajax({
                            type: "GET",
                            dataType: "json",
                            url: '/user/' + edid,
                            data: {'id': edid},
                            success: function (data) {
                                AppData.verification(data);
                                $('.username').val(data.info.username);
                                $('.password').val();
                                $('.realname').val(data.info.realname);
                                $('.phone').val(data.info.phone);
                                $('.email').val(data.info.email);
                                let departmentListObj = data.character
                                AppData.departmentList = {...departmentListObj};//把数组转成对象
                                AppData.characterid = data.info.characterid;
                                if (data.info.sex == 0) {
                                    $('#nv').prop('checked', true);
                                }
                                if (data.info.status == 0) {
                                    $('#wuxiao').prop('checked', true);
                                }
                            },
                            error: function (msg) {
                                console.log(msg);
                            }
                        });
                    },
                    end: function () { //解决弹窗关闭以后,弹窗内容还出现在下方的问题
                        $('.addCharacter').hide();
                    }
                })
            },
            deletesClick(deleteid) {
                layui.use('layer', function () {
                    layer.confirm('确认要删除吗？', {
                        btn: ['确定', '取消']//按钮
                    }, function () {
                        let ids = [];
                        ids.push(deleteid);
                        $.ajax({
                            type: "DELETE",
                            dataType: "json",
                            url: '/user/0',
                            data: {'ids': ids},
                            success: function (data) {
                                AppData.verification(data);
                                if (data.status == 1) {
                                    layer.msg(data.message, {icon: 1, time: 1000}, function () {
                                        AppData.getIndexData();
                                    });
                                } else {
                                    layer.msg(data.message, {icon: 2, time: 1000});
                                }
                            },
                            error: function (msg) {
                                console.log(msg);
                            }
                        });
                    });
                });
            },
            deleteCheckAll() {
                let checkBoxs = AppData.tableDataList;
                if ($('#deleteCheckAll').is(":checked")) {
                    for (let j in checkBoxs) {
                        $('#listcheck' + checkBoxs[j].id).prop('checked', true);
                    }
                } else {
                    for (let j in checkBoxs) {
                        $('#listcheck' + checkBoxs[j].id).prop('checked', false);
                    }
                }
            },
            deleteAllClick() {
                layui.use('layer', function () {
                    layer.confirm('确认要删除吗？', {
                        btn: ['确定', '取消']//按钮
                    }, function () {
                        let ids = [];
                        let checkBoxs = AppData.tableDataList;
                        for (let i in checkBoxs) {
                            if ($('#listcheck' + checkBoxs[i].id).is(":checked")) {
                                ids.push(checkBoxs[i].id);
                            }
                        }
                        if (ids.length == 0) {
                            layer.msg('请选择你要删除的选项');
                            return false;
                        }
                        console.log(ids.length);
                        $.ajax({
                            type: "DELETE",
                            dataType: "json",
                            url: '/user/0',
                            data: {'ids': ids},
                            success: function (data) {
                                AppData.verification(data);
                                console.log(data);
                                if (data.status == 1) {
                                    layer.msg(data.message, {icon: 1, time: 1000}, function () {
                                        AppData.getIndexData();
                                    });
                                } else {
                                    layer.msg(data.message, {icon: 2, time: 1000});
                                }
                            },
                            error: function (msg) {
                                console.log(msg);
                            }
                        });
                    });
                });
            },
            //高级搜索
            adSearchbtnClick() {
                $('.searchBox').slideToggle();
                $('.search').toggle();
            },
            resetbtnClick() {
                $('input[name="resetval"]').val('');
                $('select[name="resetval"]').val('');
            },
            addresetBtn() {
                $('input[name="resetTc"]').val('');
                $('select[name="resetTc"]').val('0');
                $('textarea[name="resetTc"]').val('');
                this.characterid = 0;
            },
            //首页列表
            getIndexData() {
                $.ajax({
                    type: "GET",
                    dataType: "json",
                    url: '/user',
                    data: {'listRows': this.listRows, 'page': this.curNum, map: this.map},
                    success: function (data) {
                        AppData.verification(data);
                        AppData.ajaxData = data;
                        AppData.totalNum = data.list.last_page;  //获取总的页面数
                        AppData.dataList = data.userInfo.jurisdiction; //所有的权限
                        let dataS = data.userInfo.jurisdiction;//后台初始数据
                        var dataArr = [];
                        for (let i in dataS) {
                            dataArr.push(dataS[i]); //属性
                        }
                        /*调用封装的改变数据结构方法*/
                        let tableData = setTreeData(dataArr);
                        let tableDataObj = {...tableData};//把转换后的数组通过扩展运算符改成对象便于遍历
                        let userList = {...data.list.data};//把转换后的数组通过扩展运算符改成对象便于遍历
                        AppData.tableData = tableDataObj;
                        AppData.tableDataList = userList;
                    },
                    error: function (msg) {
                        console.log(msg);
                    }
                });
            }
        }
    })
</script>
</body>
</html>
